package br.pro.raphael.air;

import java.util.List;

public class Routes {
	
	private List<Airport> aerodromos;
	
	public double[][] getRoutes(List<Airport> aerodromos){
		
		double matrixRota[][] = new double[aerodromos.size()][aerodromos.size()]; 
		
		for(int i=0; i<aerodromos.size();i++){
			for(int j=0; j<aerodromos.size();j++){
				Airport origem = (Airport)aerodromos.get(i);
				Airport destino = (Airport)aerodromos.get(j);
				if(origem.getIcao().compareTo(destino.getIcao()) == 0){
					matrixRota[i][j] = 0.0;
				}else{
					matrixRota[i][j] = distance(origem,destino);	
				}
				
			}
			
		}
		
		return matrixRota;
	}
	
	private static double  distance(Airport origem, Airport destino) {
		
		return distance(origem.getLatitude(), origem.getLongitude(),destino.getLatitude(), destino.getLongitude());
		
	
	}
	private static double  distance(double lat1, double lon1, double lat2, double lon2) {
		  double theta = lon1 - lon2;
		  double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
		  dist = Math.acos(dist);
		  dist = rad2deg(dist);
		  dist = dist * 60 * 1.1515;
		  dist = dist * 1.609344;
		  return (dist);
		}
	
	private static double deg2rad(double deg) {
		  return (deg * Math.PI / 180.0);
		}
	
	private static double rad2deg(double rad) {
		  return (rad * 180.0 / Math.PI);
		}

	public List<Airport> getAerodromos() {
		return aerodromos;
	}

	public void setAerodromos(List<Airport> aerodromos) {
		this.aerodromos = aerodromos;
	}

	

}
